package leetcode.code6207;

public class Solution {
	public long countSubarrays(int[] nums, int minK, int maxK) {
		long ans = 0;
		int maxi = -1, mini = -1, l = -1;
		for (int i = 0; i < nums.length; i++) {
			int num = nums[i];
			if (num > maxK || num < minK) {
				l = i;
			}
			maxi = num == maxK ? i : maxi;
			mini = num == minK ? i : mini;
			ans += Math.max(Math.min(maxi, mini) - l, 0);

		}
		return ans;
	}
}
